#define GridLoad_cxx
#include "GridLoad.h"
#include <TH2.h>
#include <TStyle.h>
#include <TCanvas.h>
#include <THStack.h>
#include <TLegend.h>
#include <iostream>
#include "GUtils.h"

using namespace std;

void GridLoad::Loop(char* sdate,char* edate,int interval)
{
   if (fChain == 0) return;

   Long64_t nentries = fChain->GetEntriesFast();

//  TCanvas* gl=new TCanvas("gl","Grid Load", 1024,768);

   Int_t nbytes = 0, nb = 0;
// 1125525600,1138147200
   pair<UInt_t,UInt_t> range=Grid::getInterval(sdate,edate);
   Long_t xmin=range.first;
   Long_t xmax=range.second;
   Long_t dt=(range.second-range.first)/interval;
   TH1F* gl_cms=new TH1F("gl_cms", "CMS",dt ,xmin,xmax);
   TH1F* gl_lhcb=new TH1F("gl_lhcb", "LHCB",dt,xmin,xmax);
   TH1F* gl_atlas=new TH1F("gl_atlas", "ATLAS",dt,xmin,xmax);
   TH1F* gl_alice=new TH1F("gl_alice", "ALICE",dt,xmin,xmax);
   TH1F* gl_others=new TH1F("gl_others", "NON LHC",dt,xmin,xmax);
   TH1F* gl_all=new TH1F("gl_all", "LCG",dt,xmin,xmax);
   THStack* st=new THStack("all","all");
//   TH1F* gl_njobs=new TH1F("gl_njobs", "Raw Number of Jobs", 122,1125530000,11320120000);

   for (Long64_t jentry=0; jentry<nentries;jentry++) {
      Long64_t ientry = LoadTree(jentry);
      if (ientry < 0) break;
      nb = fChain->GetEntry(jentry);   nbytes += nb;
   //   if (Cut(ientry) < 0) continue;
      Int_t start=gl_cms->FindBin(logmonitor_running_Epoch);
      Int_t stop=gl_cms->FindBin(logmonitor_done_Epoch);
      if(start<=stop  && start >0) {
        for(Int_t x=start;x<=stop;x++) {
  		if(VO=="cms") { gl_cms->AddBinContent(x); }
		if(VO=="lhcb")  { gl_lhcb->AddBinContent(x); }
		if(VO=="atlas") { gl_atlas->AddBinContent(x); }
       	        if(VO=="alice") { gl_alice->AddBinContent(x); }
        	 if(VO!="cms" && VO!="lhcb" && VO!="atlas" && VO!="alice") { gl_others->AddBinContent(x); }
		gl_all->AddBinContent(x);
        }
      }
    }

/*
   gl->Divide(2,3);
   gl->cd(1);
   gl_cms->Draw();
   gl->cd(2);
   gl_lhcb->Draw();
   gl->cd(3);
   gl_atlas->Draw();
   gl->cd(4);
   gl_others->Draw();
   gl->cd(5);
   gl_all->Draw();
	gl_cms->SetXTitle("Time");
	gl_atlas->SetXTitle("Time");
	gl_lhcb->SetXTitle("Time");
	gl_others->SetXTitle("Time");
	gl_all->SetXTitle("Time");
	gl_alice->SetXTitle("Time");
//	gl_njobs->SetXTitle("Time");
	gl_cms->GetXaxis()->SetTimeDisplay(1);
	gl_atlas->GetXaxis()->SetTimeDisplay(1);
	gl_lhcb->GetXaxis()->SetTimeDisplay(1);
	gl_others->GetXaxis()->SetTimeDisplay(1);
	gl_all->GetXaxis()->SetTimeDisplay(1);
	gl_alice->GetXaxis()->SetTimeDisplay(1);
//	gl_njobs->GetXaxis()->SetTimeDisplay(1);
	gl_cms->SetYTitle("N Jobs Running");
	gl_atlas->SetYTitle("N Jobs Running");
	gl_lhcb->SetYTitle("N Jobs Running");
	gl_others->SetYTitle("N Jobs Running");
	gl_all->SetYTitle("N Jobs Running");
	gl_alice->SetYTitle("N Jobs Running");
//	gl_njobs->SetYTitle("Raw Jobs Submitted");
	gl_cms->SetFillColor(1);
	gl_atlas->SetFillColor(2);
	gl_lhcb->SetFillColor(3);
	gl_others->SetFillColor(4);
	gl_all->SetFillColor(5);
	gl_alice->SetFillColor(6);
//	gl_njobs->SetFillColor(6);
*/
   gROOT->SetStyle("Plain"); 
   gStyle->SetOptStat(0);
   gl_cms->SetFillColor(2);  
   gl_alice->SetFillColor(3); 
   gl_atlas->SetFillColor(4); 
   gl_lhcb->SetFillColor(5);  
   gl_others->SetFillColor(0);
   TLegend* leg=new TLegend(0.8,0.8,0.89,0.89);
   st->Add(gl_cms);
   st->Add(gl_atlas);
   st->Add(gl_lhcb);
   st->Add(gl_alice);
   st->Add(gl_others);
   leg->AddEntry(gl_cms,"CMS","f");
   leg->AddEntry(gl_atlas,"ATLAS","f");
   leg->AddEntry(gl_alice,"ALICE","f");
   leg->AddEntry(gl_lhcb,"LHCB","f");
   leg->AddEntry(gl_others,"Others","f");
   st->Draw();
   st->GetXaxis()->SetTimeDisplay(1);
   st->GetXaxis()->SetTimeFormat("%d/%m/%y/%F1970-01-01 00:00:00");
   st->GetXaxis()->SetTitle("Time");
   st->GetYaxis()->SetTitle("Running Jobs");
   leg->Draw();
   gROOT->SetStyle("Plain"); 
   gStyle->SetOptStat(0);
 //  gl->Print("GridLoad.pdf");
}
